// let BASE_URL = "http://carlook.nanjingyunpeng.cn";
let BASE_URL = "http://admin.jinsekeji.com.cn"
// let BASE_URL = "http://10.0.0.95"


const request = (obj, data, loading, header) => {

	if (loading) uni.showLoading({
		title: "加载中",
		mask: true
	});
	let token = uni.getStorageSync("token") || ""
	let headers = {
		// "Content-type": "application/json",
		"Content-type": "application/x-www-form-urlencoded",
		"Authorization": "Bearer " + token
	}
	let httpDefaultOpts = {
		url: BASE_URL + obj.url,
		data: data,
		beforeSend: function(xmlHttp) {
			xmlHttp.setRequestHeader("If-Modified-Since", "0");
			xmlHttp.setRequestHeader("Cache-Control", "no-cache");
		},
		method: obj.method || 'POST',
		dataType: "json",
		header: headers

	};
	// console.log("0000000000",token)
	if (token) {
		if (httpDefaultOpts.header) {
			Authorization: "Bearer " + token
		}
		else {
			Authorization: "Bearer " + token
		}
	} else {
		// console.log("获取不到userToken");
	}
	let promise = new Promise(function(resolve, reject) {
		uni.request(httpDefaultOpts)
			.then((res) => {
				// console.log("res,res", res)
				if (loading) uni.hideLoading();
				if (res.statusCode == 200) {
					// console.log("请求成功",res)
					resolve(res.data);
				} else if (res.data.errcode == 1005) {
					uni.removeStorage("token")
					uni.removeStorage("userData")
					uni.showToast({
						title: res.data.errmsg,
						icon: "none",
						duration: 2000
					});
					setTimeout(() => {
						uni.reLaunch({
							url: "/pages/login/index"
						});
					}, 2500)
					uni.reLaunch({
						url: "/pages/login/index"
					});
				} else {
					reject(res);
				}
			})
			.catch((response) => {
				reject(response);
			});
	});
	return promise;
};
export default {
	BASE_URL,
	request,
};